package com.jason.system.service.impl;

import com.github.jasonj1102.aesspringbootstarter.utils.AesUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.AdminPermission;
import com.jason.system.entity.AdminRole;
import com.jason.system.entity.User;
import com.jason.system.mapper.PermissionMapper;
import com.jason.system.mapper.RoleMapper;
import com.jason.system.mapper.UserMapper;
import com.jason.system.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/22 19:59
 */

@Service
public class UserServiceImpl implements UserService {

  private static final Logger logger = LoggerFactory.getLogger(Logger.class);

  @Resource
  private UserMapper userMapper;

  @Resource
  private RoleMapper roleMapper;

  @Resource
  private PermissionMapper permissionMapper;

  @Autowired
  private AesUtils aesUtils;

  @Override
  public User getUserByName(String name) {
    User user;
    if("".equals(name)||name==null){
      user = null;
    }else {
      user = userMapper.getUserByName(name);
      logger.info("==>>>UserService==>>>初始获取user对象：{}",user);
      if(user !=null){
        AdminRole role = roleMapper.getRolesByUid(user.getUId());
        logger.info("==>>>UserService==>>>获取user对象所属角色：{}",role);
        if (role!=null){
          String permissions = permissionMapper.getPermissionsByRid(role.getRId());
          List<Integer> pIds = Arrays.stream(permissions.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
          role.setPermissions(permissionMapper.getPermissionById(pIds));
          logger.info("==>>>UserService==>>>获取role对象所属权限：{}",permissions);
        }
        user.setRole(role);
      }else {
        user = null;
      }
    }
    return user;
  }

  @Override
  public Pager<User> getAllUser(Integer page, Integer size) {
    PageHelper.startPage(page,size);
    List<User> userList = userMapper.getAllUser();
    for (User u : userList){
      AdminRole adminRole = roleMapper.getRolesByUid(u.getUId());
      u.setRole(adminRole);
    }
    PageInfo<User> userPageInfo = new PageInfo<>(userList);
    return new Pager<>(userPageInfo);
  }

  @Override
  public void register(User user) {
    try {
      user.setPassword(aesUtils.encrypt(user.getPassword()));
    } catch (Exception e) {
      e.printStackTrace();
    }
    userMapper.register(user);
    roleMapper.insertToUserRole(user.getUId(),1);
  }

  @Override
  public void updatePassword(Integer uId , String password) {
    try {
      userMapper.updatePassword(uId,aesUtils.encrypt(password));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public boolean deleteUserById(Integer uId) {
    if(uId==null){
      return false;
    }else {
      userMapper.deleteUserById(uId);
      return true;
    }
  }
}
